package com.activenetwork.smlite.testcase.daily;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.model.event.EventBasicType.Tag;
import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.schema.testcase.daily.checksmartfolderpart.CheckSmartFolderPartDataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventsHomeView;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.RandomString;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class CheckSmartFolderPart extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(CheckSmartFolderPart.class);
	private EventBusiness eventBusiness = new EventBusiness();
	private String suffix = RandomString.generateRandomString(8);
	private CheckSmartFolderPartDataType data;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup() {	
		try{
			ScriptDataBindHelper helper = new ScriptDataBindHelper(CheckSmartFolderPart.class);
			data = helper.bind(CheckSmartFolderPartDataType.class);
		}catch(IOException e){
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
		logger.info("dataSetup() complete");
	}
	
	@BeforeClass(alwaysRun = true, dependsOnMethods = {"dataSetup"})
	public void login(){
		loginBusiness.login();
		logger.info("login() complete");
	}
	
	@Test(groups = { "daily" })
	public void verifyLevel1Event(){
		logger.info("Verify level1 event can be found by level1 folder");
			
		// Delete existing tag
		EventsHomeView eventsHomewView = PageLoader.load(EventsHomeView.class);
		List<EventBasicType.Tag> tags = data.getLevel1Event().getBasic().getTag();
		for(EventBasicType.Tag tag : tags){
			eventsHomewView = eventsHomewView.deleteTag(tag.getTagName());
			logger.info("Delete all the tags start with " + tag.getTagName());
		}
		
		// Make location name and tag name unique so that 
		// new location & new tag will be created
		EventType lv1Event = data.getLevel1Event();
		uniqueLocation(lv1Event, suffix);
		uniqueTag(lv1Event, suffix);
		
		// Create the event for the smart folder
		eventBusiness.addEventUI(lv1Event);
		
		// Verify using smart folder search can find this event
		PageLoader.load(ModelPage.class).events();
		try{
			eventBusiness.searchEvent("", lv1Event);
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					CheckSmartFolderPart.class.getName(), 
					"Verify level1 event should under level1 folder", 
					"Unable to find the level1 event under level1 folder"));
		}
		
		// Delete the event
		PageLoader.load(ModelPage.class).events();
		eventBusiness.deleteEvent(lv1Event.getBasic().getTitle(), lv1Event);
	}
	
	@Test(groups = { "daily" })
	public void verifyLevel2Event(){
		logger.info("Verify level2 event can be found by level2 folder");
		
		// Delete existing tag
		EventsHomeView eventsHomewView = PageLoader.load(EventsHomeView.class);
		List<EventBasicType.Tag> tags = data.getLevel2Event().getBasic().getTag();
		for(EventBasicType.Tag tag : tags){
			eventsHomewView = eventsHomewView.deleteTag(tag.getTagName());
		}
		
		// Make location name and tag name unique so that 
		// new location & new tag will be created
		EventType lv2Event = data.getLevel2Event();
		uniqueLocation(lv2Event, suffix);
		uniqueTag(lv2Event, suffix);
		eventBusiness.addEventUI(lv2Event);
		
		// Verify using smart folder search can find this event
		PageLoader.load(ModelPage.class).events();
		try{
			eventBusiness.searchEvent("", lv2Event);
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					CheckSmartFolderPart.class.getName(), 
					"Verify level2 event should under level2 folder", 
					"Unable to find the level2 event under level2 folder"));
		}
		
		// Delete the event
		PageLoader.load(ModelPage.class).events();
		eventBusiness.deleteEvent(lv2Event.getBasic().getTitle(), lv2Event);
	}
	
	@Test(groups = { "daily" })
	public void verifyLevel3Event(){
		logger.info("Verify level3 event can be found by level3 folder");
		
		// Delete existing tag
		EventsHomeView eventsHomewView = PageLoader.load(EventsHomeView.class);
		List<EventBasicType.Tag> tags = data.getLevel3Event().getBasic().getTag();
		for(EventBasicType.Tag tag : tags){
			eventsHomewView = eventsHomewView.deleteTag(tag.getTagName());
		}
		
		// Make location name and tag name unique so that 
		// new location & new tag will be created
		EventType lv3Event = data.getLevel3Event();
		uniqueLocation(lv3Event, suffix);
		uniqueTag(lv3Event, suffix);
		eventBusiness.addEventUI(lv3Event);
		
		// Verify using smart folder search can find this event
		PageLoader.load(ModelPage.class).events();
		try{
			eventBusiness.searchEvent("", lv3Event);
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					CheckSmartFolderPart.class.getName(), 
					"Verify level3 event should under level3 folder", 
					"Unable to find the level3 event under level3 folder"));
		}
		
		// Delete the event
		PageLoader.load(ModelPage.class).events();
		eventBusiness.deleteEvent(lv3Event.getBasic().getTitle(), lv3Event);
	}
	

	
	@AfterClass
	public void logoff(){
		loginBusiness.logout();
		FailureAssertion.pop(CheckSmartFolderPart.class);
	}
	
	
	private void uniqueLocation(EventType event, String suffix){
		EventBasicType.Location location = event.getBasic().getLocation();
		String locationName = location.getLocationName()+"_"+suffix;
		location.setLocationName(locationName);
		logger.info("Add location suffix: " + locationName);
	}
	
	private void uniqueTag(EventType event, String suffix){
		List<Tag> tags = event.getBasic().getTag();
		for(Tag tag : tags){
			String tagName = tag.getTagName() + "_" + suffix;
			tag.setTagName(tagName);
			logger.info("Add tag suffix: " + tagName);
		}
	}
}
